<?php


namespace App\Http\Controllers\Api;

use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Base\Services\ConfigService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use App\Businesses\BusinessState;
use App\Http\Requests\ApiV2\AppraiseRequest;
use App\Services\Business\Models\Appraise;
use App\Services\Business\Services\AppraiseService;
use App\Services\Business\Interfaces\AppraiseServiceInterface;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class AppraiseController extends BaseController
{


    /**
     * @var AppraiseService
     */
    protected $appraiseService;
    /**
     * @var UserService
     */
    protected $userService;

    protected $businessState;


    public function __construct(
        AppraiseServiceInterface $appraiseService,
        UserServiceInterface     $userService,
        BusinessState            $businessState
    )
    {
        $this->appraiseService = $appraiseService;
        $this->userService = $userService;
        $this->businessState = $businessState;

    }



    public function appraises(Request $request, $type, $id)
    {

        $modal = ($type == 'role') ? 'App\Services\Member\Models\\'.ucfirst($type) : 'App\Services\Business\Models\\'.ucfirst($type);
        $modal = new $modal;
        $resource = $modal::find($id);
        $appraises = $resource->appraises()->orderBy('created_at', 'desc')->with(['photos'])->paginate($request->input('size', 10));
        return $this->successData(compact('appraises'));
    }

    public function recommends(Request $request)
    {

        $appraises = Appraise::query()
            ->with(['user:id,avatar,nick_name', 'user.profile'])
            ->where('is_recommend', 1)
            ->take(4)
            ->get()->toArray();
        return $this->successData(compact('appraises'));
    }





    public function choose(Request $request)
    {
        $appraiseId = $request->input('appraise_id');
        $resourceId = $request->input('resource_id');
        $resourceType = $request->input('resource_type');
        $appraise = Appraise::find($appraiseId);
        $appraise->update(['appraiseable_type' => 'App\Services\Business\Models\\' . ucfirst($resourceType), 'appraiseable_id' => $resourceId, 'is_used' => 1, 'user_id' => $this->id()]);
        $this->appraiseService->changeStatus($resourceType, $resourceId, [$this->id()]);

        return $this->success(__('提交评价成功'));
    }



    public function templates(Request $request)
    {
        $keywords = $request->input('keywords', '');
        $size = $request->input('size', 10);
        $appraises = Appraise::query()
            ->with(['images'])
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('content', 'like', '%' . $keywords . '%');
            })
            ->where('is_imported', 1)
            ->orderBy('created_at')
            ->paginate($size);
        return $this->successData(compact('appraises'));


    }



    public function contentUpdate(Request $request, $id)
    {
        $content = $request->input('content');
        $appraise = Appraise::find($id);
        $appraise->update(['content' => $content]);
        return $this->success(__('内容更新成功'));

    }


//    推荐列表

    public function store(AppraiseRequest $request, $type, $resourceId)
    {


        $modal = 'App\Services\Business\Models\\' . ucfirst($type);
        $modal = new $modal;
        $resource = $modal::find($resourceId);
        // 创建一条评论记录
        $data = $request->filldata();
        $appraise = Appraise::create($data);
        $appraise = $resource->appraises()->save($appraise);
        //改变评价状态
        $this->appraiseService->changeStatus($type, $resourceId, [Auth::id()]);
        $data['photos'] && $appraise->photos()->createMany($data['photos']);
        return $this->success(__('提交评价成功'));
    }

    public function obtain()
    {

        $userId = $this->id();

        if (Appraise::query()->where('user_id', $userId)->where('is_used', 1)->exists()) {
            return $this->error(__('您已经领取过评价'));
        }
        $appraiseIds = Appraise::query()->where('is_used', -1)->pluck('id')->toArray();
        $randomIndex = array_rand($appraiseIds);
        $randomId = $appraiseIds[$randomIndex];
        $appraise = Appraise::query()->with(['images'])->where('id', $randomId)->first();
        $appraise->update(['is_used' => 1, 'user_id' => $userId]);
        return $this->successData(compact('appraise'));


    }

    public function entrance()

    {
        $userId = $this->id();
        $appraise = [];
        /**
         * @var ConfigService $configService
         */
        $configService = app()->make(ConfigServiceInterface::class);
        $letter = $configService->getMemberLetter();
        $hasClaimed = Appraise::query()->where('user_id', $userId)->where('is_used', 1)->exists() ?: false;
        if ($hasClaimed) {
            $appraise = Appraise::query()->with(['images'])->where('user_id', $userId)->where('is_used', 1)->first() ?? [];
        }
        return $this->successData(compact('hasClaimed', 'appraise', 'letter'));
    }





//    编辑
    public function edit($id)
    {

        $appraise = Appraise::query()
            ->with(['photos'])
            ->where('id', $id)->firstOrFail();

        return $this->successData(compact('appraise'));
    }

//更新
    public function update(AppraiseRequest $request, $id)
    {

        $doubt = Appraise::query()->where('id', $id)->firstOrFail();
        $data = $request->filldata();
        $doubt->fill($data)->save();
        return $this->success(__('评价更新成功'));
    }







}
